package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Bookshelf;
import com.ruoyi.system.domain.BookshelfDetails;
import com.ruoyi.system.domain.Novel;
import com.ruoyi.system.domain.Vo.BookshelfDetailsVo;
import com.ruoyi.system.domain.dto.BookshelfDetailDTO;
import com.ruoyi.system.service.IBookshelfDetailsService;
import com.ruoyi.system.service.IBookshelfService;
import com.ruoyi.system.service.INovelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 书架详情Controller
 *
 * @author 许雅萍
 * @date 2024-11-19
 */
@Controller
@RequestMapping("/system/details")
public class BookshelfDetailsController extends BaseController {
    private String prefix = "system/details";
    @Resource
    private IBookshelfService bookshelfService;
    @Resource
    private INovelService novelService;

    @Autowired
    private IBookshelfDetailsService bookshelfDetailsService;


    @GetMapping()
    public String details() {
        return prefix + "/details";
    }

    /**
     * 查询书架详情列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BookshelfDetails bookshelfDetails) {
        startPage();
        List<BookshelfDetails> list = bookshelfDetailsService.selectBookshelfDetailsList(bookshelfDetails);
        return getDataTable(list);
    }

    /**
     * 导出书架详情列表
     */

    @Log(title = "书架详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BookshelfDetails bookshelfDetails) {
        List<BookshelfDetails> list = bookshelfDetailsService.selectBookshelfDetailsList(bookshelfDetails);
        ExcelUtil<BookshelfDetails> util = new ExcelUtil<BookshelfDetails>(BookshelfDetails.class);
        return util.exportExcel(list, "书架详情数据");
    }

    /**
     * 新增书架详情
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        Bookshelf bookshelf = new Bookshelf();
        bookshelf.setUserId(ShiroUtils.getUserId());
        List<Bookshelf> bookshelves = bookshelfService.selectBookshelfList(bookshelf);

        List<Novel> novels = novelService.selectNovelList(new Novel());

        modelMap.put("bookshelves", bookshelves);
        modelMap.put("novels", bookshelves);
        return prefix + "/add";
    }

    /**
     * 新增保存书架详情
     */

    @Log(title = "书架详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BookshelfDetails bookshelfDetails) {
        SysUser sysUser = ShiroUtils.getSysUser();
        bookshelfDetails.setUserId(sysUser.getUserId());
        return toAjax(bookshelfDetailsService.insertBookshelfDetails(bookshelfDetails));
    }

    /**
     * 书架加书
     * @param bookshelfDetailDTO
     * @return
     */
    @PostMapping("/createBookShelfDetails")
    @ResponseBody
    public AjaxResult createBookShelfDetails(@RequestBody BookshelfDetailDTO bookshelfDetailDTO) {
        List<String> books = bookshelfDetailDTO.getBooks();
        String shelfId = bookshelfDetailDTO.getShelfId();
        SysUser sysUser = ShiroUtils.getSysUser();
        int count = 0;
        for (String id : books) {

            BookshelfDetails build = BookshelfDetails.builder()
                    .bookshelfId(Long.valueOf(shelfId))
                    .novelId(Long.valueOf(id))
                    .userId(sysUser.getUserId())
                    .build();

            int i = bookshelfDetailsService.insertBookshelfDetails(build);
            count = i + count;
        }


        return toAjax(count);
    }

    /**
     * 修改书架详情
     */
    @GetMapping("/readBook/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BookshelfDetails build = BookshelfDetails.builder()
                .bookshelfId(id)
                .build();
        List<BookshelfDetails> bookshelfDetails = bookshelfDetailsService.selectBookshelfDetailsList(build);

        List<BookshelfDetailsVo> bookshelfDetailsVos = new ArrayList<>();

        for (BookshelfDetails bookshelfDetail : bookshelfDetails) {
            Novel novel = novelService.selectNovelByNovelId(bookshelfDetail.getNovelId());
            Bookshelf bookshelf = bookshelfService.selectBookshelfById(bookshelfDetail.getBookshelfId());

            BookshelfDetailsVo build1 = BookshelfDetailsVo.builder()
                    .bookshelfName(bookshelf.getName())
                    .title(novel.getTitle())
                    .build();

            bookshelfDetailsVos.add(build1);
        }

        mmap.put("bookshelfDetailsVos", bookshelfDetailsVos);
        return prefix + "/shelfBooks";
    }

    /**
     * 修改保存书架详情
     */

    @Log(title = "书架详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BookshelfDetails bookshelfDetails) {
        SysUser sysUser = ShiroUtils.getSysUser();
        bookshelfDetails.setUserId(sysUser.getUserId());
        return toAjax(bookshelfDetailsService.updateBookshelfDetails(bookshelfDetails));
    }

    /**
     * 删除书架详情
     */

    @Log(title = "书架详情", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bookshelfDetailsService.deleteBookshelfDetailsByIds(ids));
    }
}
